package org.ysh.demo.controller;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.ysh.demo.vo.ApiResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/archive")
public class ArchiveApiController {

    @Resource
    RestTemplate restTemplate;

    /**
     * 这个必须给档案系统，用于回调给予code，该code在申请token时必须用到
     */
    String ME_PATH = "http://localhost:8082/shiro/archive/getAppCode";

    /**
     * 档案系统给的，可以简单理解为用户名和密码
     */
//    String appId = "043a31f5bbd0a733ddda28ea5149f50a7b574df4436000bfe244c042c653a4fb";
//    String appSecret= "674d087abb30a174dcb720842ccbd48aad48a7926c646ae9c0b3b6a2d7e786f5";


    String appId = "e0eca445e3430bc86eeef0be28252b9a94abb8ed873031a62d36ab2aa3ab6abc";
    String appSecret= "2a19c60cc359b8e9798f4b01aaf41256b5018f4445ecd48b2193a7dd4820a07c";

    @RequestMapping("/getAppCode")
    public ApiResult getAppCode(String code){
        System.out.println("code=>"+ code);

        String state = System.currentTimeMillis() + "";
        //接收到code 立刻用于获取 token
        String url = "http://localhost:8080/darj/api/oauth/companyToken?appId=" + appId + "&appSecret=" + appSecret + "&code=" + code + "&redirectUri=" + ME_PATH + "&state=" + state;

        ResponseEntity<Map> mapResponseEntity = restTemplate.postForEntity(url, null, Map.class, Collections.EMPTY_MAP);
        //这个结果Map中会包含companyToken及companyId等信息
        System.out.println(mapResponseEntity.getBody().entrySet());
        return ApiResult.success("获取档案系统授权成功");
    }



}
